#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 代码来自eurasia项目，源文件：http://code.google.com/p/eurasia/source/browse/trunk/src/eurasia/pyetc.py
# 感谢作者：eishn
# http://eishn.blog.163.com/blog/static/6523182007236721876/

# pyetc.py
# Python 格式的配置文件支持库
#
import sys
import os.path
import locale

Module = type(sys)
modules = {}
reload(sys)
sys.setdefaultencoding('utf-8')
SYS_ENCODING = locale.getdefaultlocale()[1]

def load(fullpath, env={}, module=Module):
        fullpath = fullpath.decode('utf-8').encode(SYS_ENCODING)
        try:
                code = open(fullpath).readlines()
        except IOError:
                raise ImportError, 'No module named  %s' %fullpath

        code = '\n'.join(i.rstrip() for i in code)
        filename = os.path.basename(fullpath)

        try:
                return modules[filename]
        except KeyError:
                pass

        m = module(filename)
        m.__module_class__ = module
        m.__file__ = fullpath

        m.__dict__.update(env)

        exec compile(code, filename, 'exec') in m.__dict__
        modules[filename] = m

        
        return m

def unload(m):
        filename = os.path.basename(m.__file__)
        del modules[filename]

        return None

def reload(m):
        fullpath = m.__file__

        try:
                code = open(fullpath).read()
        except IOError:
                raise ImportError, 'No module named  %s' %fullpath

        code = '\n'.join(i.rstrip() for i in code)

        env = m.__dict__
        module_class = m.__module_class__

        filename = os.path.basename(fullpath)
        m = module_class(filename)

        m.__file__ = fullpath
        m.__dict__.update(env)
        m.__module_class__ = module_class

        exec compile(code, filename, 'exec') in m.__dict__
        modules[filename] = m

        return m

